package com.sec.foodit;

import java.io.File;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.format.Time;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.Toast;

public class PostFormActivity extends Activity {
	
	private ActivityManager am = ActivityManager.getInstance();

	private ImageView photo;
	private EditText name;
	private RatingBar point;
	private EditText comment;

	private boolean isTouchedName = false;
	private boolean isTouchedComment = false;
	private Uri photoUri = null; 
	private String path = null;
	
	private Bitmap bitmap = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_post_form);

		getActionBar().hide();
		am.addActivity(this);

		photo = (ImageView)findViewById(R.id.photo);
		name = (EditText)findViewById(R.id.name);
		point = (RatingBar)findViewById(R.id.point);
		comment = (EditText)findViewById(R.id.comment);

		name.setOnFocusChangeListener(new View.OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!isTouchedName)
				{
					name.setText("");
					isTouchedName = true;
					Log.e("foodit","onCLickName");
				}
			}
		});

		comment.setOnFocusChangeListener(new View.OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!isTouchedComment)
				{
					comment.setText("");
					isTouchedComment = true;
					Log.e("foodit","onCLickComment");
				}
			}
		});


	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_post_form, menu);
		//startActivity(new Intent("com.sec.foodit.PostViewActivity"));
		return true;
	}

	public void onClickPhoto(View v)
	{
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType("image/*");
		startActivityForResult(intent, 1);
	}
	public void onSubmit(View v)
	{
		DataManager.getInstance().pList.add(new Post(name.getText().toString(), "iqjumps", 
				path, point.getRating(), comment.getText().toString(), "2013-06-13"));
		DataManager.getInstance().curRestName = name.getText().toString();
		startActivity(new Intent("com.sec.foodit.RestActivity"));
		
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		try
		{
			if(requestCode == 1 && resultCode == RESULT_OK)
			{
				photoUri = intent.getData();
				
				/* 선택된 id로 MediaDB에서 file path를 얻는다. */
				Cursor cursor = getContentResolver().query(intent.getData(), null, null, null, null);
				cursor.moveToNext();
				String path = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
				Uri uri = Uri.fromFile(new File(path));
				Log.e("foodit", "## Image Path: " + uri.getPath());
				path = uri.getPath();
				cursor.close();
	
				/* 이미지 사이즈를 먼저 조사해 적절한 사이즈로 축소해서 Bitmap을 생성한다. */
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inJustDecodeBounds = true;
				BitmapFactory.decodeFile(uri.getPath(), options);
				options.inSampleSize = calculateInSampleSize(options, photo.getWidth(), photo.getHeight());
				options.inJustDecodeBounds = false;
				bitmap = BitmapFactory.decodeFile(uri.getPath(), options);
	
				float width = bitmap.getWidth();
				float height = bitmap.getHeight();
				Log.e("foodit","## Bitmap Resized Size : " + width + " X " + height + " (" + bitmap.getByteCount() + " bytes)");
				
				photo.setImageBitmap(bitmap);
			}
		}
		catch(Exception e)
		{
			Log.e("foodit","onActivityResult::error: " + e.toString());
		}
	}
	
	/* 비트맵 샘플링 사이즈 결정 */
	public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
	{    
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) 
		{	
			final int heightRatio = Math.round((float) height / (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);
			inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
		}
		return inSampleSize;
	}

	@Override
	protected void onDestroy() {
		photo.setImageBitmap(null);
		if(bitmap != null)
			bitmap.recycle();
		bitmap = null;
		super.onDestroy();
		
		am.removeActivity(this);
	}
	
	

}
